package com.example.broadcastlistener;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.lang.ref.WeakReference;
import java.util.LinkedList;

/**
 * @author hdz
 * @date 2019/06/22
 */
public class MessageDetailAdapter extends BaseAdapter {
    private LinkedList<MessageDetail> mData;
    private WeakReference<Context> mWeakContext;

    public MessageDetailAdapter(LinkedList<MessageDetail> data, Context context) {
        this.mWeakContext = new WeakReference<>(context);
        this.mData = data;
    }

    public void add(MessageDetail data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }
    public void add(int position, MessageDetail data){
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(position, data);
        notifyDataSetChanged();
    }
    public void remove(MessageDetail data) {
        if(mData != null) {
            mData.remove(data);
        }
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if(mData != null) {
            mData.remove(position);
        }
        notifyDataSetChanged();
    }

    public void removeAll() {
        if(mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        if (mData != null) {
            return mData.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(mWeakContext.get()).inflate(R.layout.item_message_detail,parent,false);
            holder = new ViewHolder();
            holder.tvActionName = (TextView) convertView.findViewById(R.id.tvActionName);
            holder.tvDetail = (TextView) convertView.findViewById(R.id.tvDetail);
            convertView.setTag(holder);
            convertView.setBackgroundColor(mWeakContext.get().getResources().getColor(R.color.color_2c2c2c));
        } else {
            holder = (ViewHolder) convertView.getTag();
            if (position%2 == 0) {
                convertView.setBackgroundColor(mWeakContext.get().getResources().getColor(R.color.color_2c2c2c));
            } else {
                convertView.setBackgroundColor(mWeakContext.get().getResources().getColor(R.color.color_515151));
            }
        }

        holder.tvActionName.setText(mData.get(position).getActionName());
        holder.tvDetail.setText(mData.get(position).getDetail());

        return convertView;
    }

    private static class ViewHolder{
        TextView tvActionName;
        TextView tvDetail;
    }
}
